package com.hnqcgc.sgzShopping.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLWarning;

public class MyTrans {
    boolean isAuto;
    Connection conn;
    public MyTrans(boolean isAuto) {
        IDb db=new DbImpl();
        try {
            conn= db.getConn();
            conn.setAutoCommit(isAuto);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //自己提交
    public void commit(){
        if(conn!=null){
            try {
                conn.commit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //自己回滚
    public void rollback(){
        if(conn!=null){
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //关闭数据库
    public void closeConn(){
        try {
            if(conn!=null&&!conn.isClosed()){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    //返回这个数据库连接
    public Connection getConnection()throws SQLException{
        return conn;
    }


}
